package org.amphiprion.descentv2assistant.ui.popup;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.amphiprion.descentv2assistant.ApplicationConstants;
import org.amphiprion.descentv2assistant.GameSessionActivity;
import org.amphiprion.descentv2assistant.R;
import org.amphiprion.descentv2assistant.adapter.PhotoAdapter;
import org.amphiprion.descentv2assistant.dao.db.GameSessionDao;
import org.amphiprion.descentv2assistant.entity.GameSession;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Gallery;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class EncounterOverlordPopupWindow extends PopupWindow {
	private final SherlockFragmentActivity mContext;
	private final View view;

	private final View parent;
	private final Gallery gallery;
	private final File photosDirectoryPhoto;
	private PhotoAdapter adapter;
	private final TextView txtNotes;
	private final TextView txtXp;

	public EncounterOverlordPopupWindow(SherlockFragmentActivity paramSherlockFragmentActivity, final GameSession gameSession, View parent) {
		super(paramSherlockFragmentActivity);
		this.mContext = paramSherlockFragmentActivity;
		this.view = LayoutInflater.from(paramSherlockFragmentActivity).inflate(R.layout.popup_encounter_overlord_detail, null);
		setContentView(this.view);
		this.parent = parent;
		setHeight(-2);
		setWidth(-2);
		setOutsideTouchable(true);
		setFocusable(true);
		setBackgroundDrawable(new BitmapDrawable(paramSherlockFragmentActivity.getResources()));
		// getCardContent(paramSherlockFragmentActivity);

		photosDirectoryPhoto = new File(Environment.getExternalStorageDirectory() + "/" + ApplicationConstants.DIRECTORY + "/photos/" + gameSession.getName().replaceAll("'", "_") + "_" + gameSession.getId() + "/overlord/");
		photosDirectoryPhoto.mkdirs();

		TextView btAddPhoto = (TextView) view.findViewById(R.id.add_photo);
		btAddPhoto.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
				File img = new File(photosDirectoryPhoto, timeStamp + ".jpg");
				Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(img));
				mContext.startActivityForResult(takePictureIntent, GameSessionActivity.REQUEST_CODE_TAKE_PHOTO_OVERLORD);
			}
		});

		TextView btRemovePhoto = (TextView) view.findViewById(R.id.remove_photo);
		btRemovePhoto.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (adapter != null) {
					String img = (String) gallery.getSelectedItem();
					// Log.d(ApplicationConstants.PACKAGE, "img=" + img);
					if (img != null && new File(photosDirectoryPhoto, img).delete()) {
						adapter = new PhotoAdapter(mContext, photosDirectoryPhoto);
						gallery.setAdapter(adapter);

					}

				}
			}
		});

		TextView btViewPhoto = (TextView) view.findViewById(R.id.view_photo);
		btViewPhoto.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (adapter != null) {
					String img = (String) gallery.getSelectedItem();
					// Log.d(ApplicationConstants.PACKAGE, "img=" + img);
					if (img != null) {
						File f = new File(photosDirectoryPhoto, img);

						Intent intent = new Intent();
						intent.setAction(android.content.Intent.ACTION_VIEW);
						intent.setDataAndType(Uri.fromFile(f), "image/*");
						mContext.startActivity(intent);
					}

				}
			}
		});

		gallery = (Gallery) view.findViewById(R.id.gallery);
		adapter = new PhotoAdapter(mContext, photosDirectoryPhoto);
		gallery.setAdapter(adapter);

		txtNotes = (TextView) view.findViewById(R.id.txtNotes);
		String s = GameSessionDao.getInstance(mContext).readNotes(gameSession);
		if (s != null) {
			txtNotes.setText(s);
		}

		this.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				String s = "" + txtNotes.getText();
				GameSessionDao.getInstance(mContext).writeNotes(gameSession, s);

				int oldXp = gameSession.getOverlordXp();
				s = "" + txtXp.getText();
				if (s.length() > 0) {
					gameSession.setOverlordXp(Integer.parseInt(s));
				}

				if (oldXp == gameSession.getOverlordXp()) {
				} else {
					GameSessionDao.getInstance(mContext).persist(gameSession);
					// EncounterQuestListFragment.sThis.initData();
				}

			}
		});

		txtXp = (TextView) view.findViewById(R.id.txtXp);
		txtXp.setText("" + gameSession.getOverlordXp());

	}

	public void show() {
		showAtLocation(this.parent, 17, 0, 0);
	}

	public void photoTaken(Intent data) {
		adapter = new PhotoAdapter(mContext, photosDirectoryPhoto);
		gallery.setAdapter(adapter);

	}

}
